'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: paginator.py
* @Time: 2025/11/11
* @All Rights Reserve By Brtc
'''
import math
from dataclasses import dataclass
from typing import Any
from flask_wtf import FlaskForm
from wtforms import IntegerField
from wtforms.validators import Optional, NumberRange
from pkg.sqlalchemy import SQLAlchemy


class PaginatorReq(FlaskForm):
    """分页请求基础数据, 涵盖当前页数、每条页数、如果请求接口需要携带分页信息， 可直接集成该类"""
    current_page = IntegerField("current_page",default=1,  validators=[
        Optional(),
        NumberRange(min=1, max=999, message="当前页面的范围支持 1-999 页.")
    ])
    page_size = IntegerField("page_size",default=20, validators=[
        Optional(),
        NumberRange(min=1, max=50, message="一个页面最多展示1-50条数据.")
    ])

@dataclass
class Paginator:
    """分页器"""
    total_page: int = 0 # 总页数
    total_record: int = 0 # 总条数
    current_page: int = 0 #当前页数
    page_size: int = 20 # 每页条数

    def __init__(self, db:SQLAlchemy, req:PaginatorReq=None):
        if req is not None:
            self.current_page = req.current_page.data
            self.page_size = req.page_size.data
        self.db = db


    def paginate(self, select)->list[Any]:
        """对传入的查询进行分页"""
        #1、调用 db.paginator 进行数据分页
        p = self.db.paginate(select, page=self.current_page, per_page=self.page_size, error_out=False)
        #2、计算 总页数  + 总条数
        self.total_record = p.total
        self.total_page = math.ceil(p.total/self.page_size)
        #3、返回分页数据
        return p.items


@dataclass
class PageModel:
    list:list[Any]
    paginator:Paginator